package com.xinziruo.crawl.domain.crawl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.xinziruo.crawl.domain.util.AbstractDomain;
import com.xinziruo.crawl.domain.util.Duplicable;
import com.xinziruo.crawl.infrastruture.util.EntityUtils;

/**
 * 
 * @author loudyn
 * @modify ajian
 * 
 */
public class CrawlApiObject extends AbstractDomain implements Duplicable<CrawlApiObject> {
	private static final long serialVersionUID = 1L;
	private String name;
	private String enterUrl;
	private String itemsExpression;
	private String classify;
	private String project;
	private boolean followingItem = false;
	private String payloadString;
	private String callbackUrl;

	private String cronExpression;

	private List<CrawlApiExpression> parseExpressions = new ArrayList<CrawlApiExpression>();
	private List<CrawlApiParam> params = new ArrayList<CrawlApiParam>();

	
	public String getProject() {
		return project;
	}

	public void setProject(String project) {
		this.project = project;
	}

	public String getClassify() {
		return classify;
	}

	public void setClassify(String classify) {
		this.classify = classify;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEnterUrl() {
		return enterUrl;
	}

	public void setEnterUrl(String enterUrl) {
		this.enterUrl = enterUrl;
	}

	public String getItemsExpression() {
		return itemsExpression;
	}

	public void setItemsExpression(String itemsExpression) {
		this.itemsExpression = itemsExpression;
	}

	public boolean isFollowingItem() {
		return followingItem;
	}

	public void setFollowingItem(boolean followingItem) {
		this.followingItem = followingItem;
	}

	public String getPayloadString() {
		return payloadString;
	}

	public void setPayloadString(String payloadString) {
		this.payloadString = payloadString;
	}

	public String getCallbackUrl() {
		return callbackUrl;
	}

	public void setCallbackUrl(String callbackUrl) {
		this.callbackUrl = callbackUrl;
	}

	public String getCronExpression() {
		return cronExpression;
	}

	public void setCronExpression(String cronExpression) {
		this.cronExpression = cronExpression;
	}

	public List<CrawlApiExpression> getParseExpressions() {
		return parseExpressions;
	}

	public void setParseExpressions(List<CrawlApiExpression> parseExpressions) {
		this.parseExpressions = parseExpressions;
	}

	public CrawlApiObject addParseExpression(CrawlApiExpression expr) {
		getParseExpressions().add(expr);
		return this;
	}

	public List<CrawlApiParam> getParams() {
		return params;
	}

	public void setParams(List<CrawlApiParam> params) {
		this.params = params;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.youboy.crawl.domain.shared.Duplicable#duplicate()
	 */
	@Override
	public CrawlApiObject duplicate() {
		return (CrawlApiObject) EntityUtils.clone(this);
	}

	public CrawlApiObject verifyParseExpressions() {
		for (CrawlApiExpression expr : getParseExpressions()) {
			if (StringUtils.isBlank(expr.getId())) {
				expr.setId(null);
			}

			expr.setApi(this);
			expr.verifyStringFilterConfigs();
		}

		return this;
	}

	public boolean hasParams() {
		return !getParams().isEmpty();
	}

	public CrawlApiObject verifyParams() {
		for (CrawlApiParam param : getParams()) {
			if (StringUtils.isBlank(param.getId())) {
				param.setId(null);
			}
			param.setApi(this);
		}

		return this;
	}

	public CrawlApiObject addParam(CrawlApiParam param) {
		getParams().add(param);
		return this;
	}
 
}
